package com.android.pdd.baseapp.app;

import android.app.Application;
import android.util.Log;

import com.google.gson.Gson;
import com.just.agentweb.AgentWebConfig;
import com.just.agentweb.LogUtils;
import com.lg.meng.application.StubApplication;

import okhttp3.OkHttpClient;

/**
 * Author: xx
 * Date: 2019/11/07
 * Description:
 */
public class App extends Application {
    public static Gson gson;
    public static App instance;
    private OkHttpClient okHttpClient;

    @Override
    public void onCreate() {
        super.onCreate();

        gson = new Gson();
        instance = this;
        StubApplication.init(instance);
        okHttpClient = new OkHttpClient.Builder().followRedirects(false).followSslRedirects(false)
                .build();
    }

    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public static String getSuningPingouCookie() {
//        AgentWebConfig.syncCookie("https://passport.suning.com","route=51a6393e60af62776d61ac8cff9b38b7; _device_session_id=p_362120cb-6ec4-4241-a318-5add1633e30e; hm_guid=ba76a79a-0ea8-4c9d-95d9-55e75756d4ce; _df_ud=71f86805-8311-4927-a08d-14532c7bdb95; _cp_dt=209b68e8-668e-4091-8539-a97b4679b991-03900; tradeMA=169; custLevel=161000000100; custno=7038322345; idsLoginUserIdLastTime=; logonStatus=2; nick=177******48; nick2=177******48; sncnstr=L1x7iKLqMsiBOci010XMkA%3D%3D; new_cityCode=021; _snvd=1583340347580OvOlcpU2g9a; provinceCode=140; cityCode=795; districtCode=08; townCode=7950899; addrNum=; address=%7B%22provName%22%3A%22%E6%B1%9F%E8%A5%BF%E7%9C%81%22%2C%22cityName%22%3A%22%E5%AE%9C%E6%98%A5%E5%B8%82%22%2C%22districtName%22%3A%22%E4%B8%B0%E5%9F%8E%E5%B8%82%22%2C%22address%22%3A%22%E5%A4%AA%E9%98%B3%E8%B7%AF%E4%B8%8E%E6%B2%B3%E5%B7%9E%E8%A1%97%E4%BA%A4%E6%B1%87%E5%A4%84%22%2C%22locLat%22%3A%2228.174867%22%2C%22locLng%22%3A%22115.778859%22%2C%22store%22%3A%22%22%7D; province=140; cityId=9192; city=1000192; streetCode=7950800; authId=si926A5D9067CC22AD163CC8AC89B51F70; secureToken=0261AE2233C6D41A87EB65D2DFA4A844; ids_r_me=NzAzODMyMjM0NV9CUk9XU0VSXzE1ODMzNDAyOTgyMjVfMTU4MzUxNzcyOTE2M18zX2MzNzI5ZDY0%0D%0ANDA5Y2Q5OTUwMGM2MDdiODIzM2VjMDE1%0D%0A; _snmc=1; _snsr=direct%7Cdirect%7C%7C%7C; _snzwt=THmckd170b1030c13yWmpe246; _snma=1%7C15833402850999429%7C1583340285099%7C1583517737730%7C1583517754377%7C109%7C4; _snmp=158351775388297409; _snmb=158351773035223402%7C1583517754418%7C1583517754387%7C3");
//        AgentWebConfig.syncCookie("https://suning.com","route=51a6393e60af62776d61ac8cff9b38b7; _device_session_id=p_362120cb-6ec4-4241-a318-5add1633e30e; hm_guid=ba76a79a-0ea8-4c9d-95d9-55e75756d4ce; _df_ud=71f86805-8311-4927-a08d-14532c7bdb95; _cp_dt=209b68e8-668e-4091-8539-a97b4679b991-03900; tradeMA=169; custLevel=161000000100; custno=7038322345; idsLoginUserIdLastTime=; logonStatus=2; nick=177******48; nick2=177******48; sncnstr=L1x7iKLqMsiBOci010XMkA%3D%3D; new_cityCode=021; _snvd=1583340347580OvOlcpU2g9a; provinceCode=140; cityCode=795; districtCode=08; townCode=7950899; addrNum=; address=%7B%22provName%22%3A%22%E6%B1%9F%E8%A5%BF%E7%9C%81%22%2C%22cityName%22%3A%22%E5%AE%9C%E6%98%A5%E5%B8%82%22%2C%22districtName%22%3A%22%E4%B8%B0%E5%9F%8E%E5%B8%82%22%2C%22address%22%3A%22%E5%A4%AA%E9%98%B3%E8%B7%AF%E4%B8%8E%E6%B2%B3%E5%B7%9E%E8%A1%97%E4%BA%A4%E6%B1%87%E5%A4%84%22%2C%22locLat%22%3A%2228.174867%22%2C%22locLng%22%3A%22115.778859%22%2C%22store%22%3A%22%22%7D; province=140; cityId=9192; city=1000192; streetCode=7950800; authId=si926A5D9067CC22AD163CC8AC89B51F70; secureToken=0261AE2233C6D41A87EB65D2DFA4A844; ids_r_me=NzAzODMyMjM0NV9CUk9XU0VSXzE1ODMzNDAyOTgyMjVfMTU4MzUxNzcyOTE2M18zX2MzNzI5ZDY0%0D%0ANDA5Y2Q5OTUwMGM2MDdiODIzM2VjMDE1%0D%0A; _snmc=1; _snsr=direct%7Cdirect%7C%7C%7C; _snzwt=THmckd170b1030c13yWmpe246; _snma=1%7C15833402850999429%7C1583340285099%7C1583517737730%7C1583517754377%7C109%7C4; _snmp=158351775388297409; _snmb=158351773035223402%7C1583517754418%7C1583517754387%7C3");
        String cookies = AgentWebConfig.getCookiesByUrl("https://passport.suning.com");
        Log.e("AppToken", "cookies:\n" + cookies);

        LogUtils.e("AppToken", AgentWebConfig.getCookiesByUrl("https://suning.com"));
        if (cookies != null && cookies.contains("authId")) {
            StringBuffer sb = new StringBuffer();
            String[] strings = cookies.split(";");
            if (strings != null && strings.length > 0) {
                for (String str : strings) {
                    if (str.contains("authId")) {
                        sb.append(str + ";");
                    }
                    if (str.contains("custno")) {
                        sb.append(str + ";");
                    }
                    if (str.contains("_device_session_id")) {
                        sb.append(str + ";");
                    }
                    if (str.contains("_snzwt")) {
                        sb.append(str + ";");
                    }
                }
                return cookies;
            }
        }
        return "";
    }

    public boolean checkCookie(String cookie) {
        return cookie.contains("authId") && (cookie.contains("ids_r_me") || (cookie.contains("TGC=") && cookie.contains("tradeMA=")));
    }
}
